package com.example.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisDemoSet {
    public static void test(Jedis jedis) {
        jedis.flushAll();

        System.out.println("sadd 和 smembers 使用");
        jedis.sadd("key","a","b","c","d");
        Set<String> smembers = jedis.smembers("key");
        System.out.println("key: " +smembers);

        System.out.println("sismember ， scard ， spop  使用");
        boolean result = jedis.sismember("key", "a");
        System.out.println("result: " + result);
        long len  = jedis.scard("key");
        System.out.println("key len: " +len);
        jedis.spop("key");
        System.out.println("key len: " +jedis.scard("key"));

        System.out.println("sinter 和 sinterstore");

        jedis.sadd("key2","1","2","3","4","5");
        jedis.sadd("key3","3","4","5","6","7");
        System.out.println("[key2 key3]sinter: "+ jedis.sinter("key2","key3"));

        long sinterstore = jedis.sinterstore("key4", "key2", "key3");
        System.out.println("sinterstore: " + sinterstore);
        System.out.println("key4: " + jedis.smembers("key4"));
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            test(jedis);
        }
    }
}
